﻿using System;
using System.Threading;
using System.Threading.Tasks;

using Google;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
/*
 * https://developers.google.com/drive/web/quickstart/quickstart-cs
 * 
 * https://developers.google.com/drive/web/quickstart/quickstart-cs#optional_view_a_quickstart_video
 * 
 * Install-Package Google.Apis.Drive.v2 -Pre
 * 
 * 
 * View more at:
 * https://developers.google.com/drive/web/examples/dotnet
 * https://developers.google.com/drive/web/
 * https://developers.google.com/drive/web/examples/dotnet
 * https://developers.google.com/drive/web/about-sdk
 * http://stackoverflow.com/questions/18897662/c-google-drive-google-apis-services-are-you-missing-an-assembly-or-reference
 */
namespace NewGoogleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //https://console.developers.google.com/project
            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = "1017618828222.apps.googleusercontent.com",
                    ClientSecret = "4QbZ7sQ0nkhK1mF9Q4aaaPfZ",
                },
                new[] { DriveService.Scope.Drive },
                "user",
                CancellationToken.None).Result;

            // Create the service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "API Project",
            });

            //File Metadaat
            File body = new File();
            body.Title = "My document";
            body.Description = "A test document";
            body.MimeType = "text/plain";

            //File Content
            byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
            request.Upload();

            File file = request.ResponseBody;
            Console.WriteLine("File id: " + file.Id);
            Console.WriteLine("Press Enter to end this process.");
            Console.ReadLine();
        }
    }
}